package S13.first;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class F01 {

	public static void main(String[] args) {
		List<Person>list=new ArrayList<Person>();
		list.add(new Person("zhangsan",19));
		list.add(new Person("lisi",22));
		list.add(new Person("wangwu",18));
		list.add(new Person("yangliu",20));
		System.out.println("集合中的所有记录：");
		Iterator<Person> it = list.iterator(); //获取迭代器对象
		while(it.hasNext()) { //是否还有数据
			Person p1 = it.next();//获取数据
			System.out.println("\t"+p1);//显示数据
		}
		//利用迭代器来删除年龄小于20岁的元素
		it = list.iterator();
		while(it.hasNext()) {
			Person p1 = it.next();//获取数据
			if(p1.getAge()<20) it.remove(); //删除元素
		}
		System.out.println("删除年龄小于20岁的元素后的集合内容：");
		for(Person p1:list) {
			//if(p1.getAge()<=20)list.remove(p1); //增强的for循环中不删除元素
			System.out.println("\t"+p1);
		}
		for(int i=list.size()-1; i>=0; --i) { //可以的
			Person p1 = list.get(i);
			if(p1.getAge()<=20)list.remove(i);
		}
		System.out.println(list);

	}

}
